package fr.auzaiphe.admin.view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.SpringLayout;
import javax.swing.SwingConstants;

import fr.auzaiphe.admin.constantes.ConstantesAdmin;
import fr.auzaiphe.admin.controller.AdminController;
import fr.auzaiphe.admin.model.AdminModel;
import fr.auzaiphe.admin.model.Evenement;
import fr.auzaiphe.commun.NonEditableDefaultTableModel;
import fr.auzaiphe.commun.Outils;

public class AdminPanel extends JPanel
{
	private AdminController controller;

	private JPanel panelGauche;
	private JPanel panelDroit;

	private SpringLayout layout;
	private SpringLayout layoutGauche;
	private JPanel panelChrono;
	private JComboBox comboBox;
	private JLabel labelChrono;
	private SpringLayout layoutDroit;
	private SpringLayout layoutChrono;

	private JButton btnPause;
	private JButton btnPlay;

	private JButton btnOn;

	private JTextArea textAreaChat;
	private JButton buttonSend;
	private JTextField fieldChat;

	private JTabbedPane tabbedPane;

	private JScrollPane scrollPane1;
	private JScrollPane scrollPane2;
	private JScrollPane scrollPane3;
	private JScrollPane scrollPane4;

	private JPanel ongletIndice;
	private SpringLayout sOngletIndice;
	private JButton btnNewIndice;
	private JTable tableIndice;
	private NonEditableDefaultTableModel tableurIndice;
	private JTextField textFieldPrix;
	private JLabel labelPrixIndice;
	private JButton btnValiderPrix;

	private JPanel ongletAlerte;
	private SpringLayout sOngletAlerte;
	private JButton btnNewAlerte;
	private JTable tableAlerte;
	private NonEditableDefaultTableModel tableurAlerte;

	private JPanel ongletJoueur;
	private SpringLayout sOngletJoueur;
	private JButton btnNewJoueur;
	private JTable tableJoueur;
	private NonEditableDefaultTableModel tableurJoueur;

	private JPanel ongletScenario;
	private SpringLayout sOngletScenario;
	private JButton btnSaveScenario;
	private JTextArea txtScenario;
	private JLabel labelDureePartie;
	private JFormattedTextField fieldHeure;
	private JFormattedTextField fieldMinute;

	private JPanel ongletNotifications;
	private SpringLayout sOngletNotifications;
	private NotificationPanel notificationBar;
	private JScrollPane notifPane;

	private ListSelectionModel listSelectionModelJoueur;

	private ListSelectionModel listSelectionModelIndice;

	private int numOngletNotification;

	public AdminPanel( AdminController pController )
	{
		super();
		this.controller = pController;

		this.layout = new SpringLayout();
		this.setLayout( this.layout );

		this.panelGauche = new JPanel();
		this.layoutGauche = new SpringLayout();
		this.panelGauche.setLayout( this.layoutGauche );
		this.layout.putConstraint( SpringLayout.WEST, this.panelGauche, 5, SpringLayout.WEST, this);
		this.layout.putConstraint( SpringLayout.EAST, this.panelGauche, -260, SpringLayout.EAST, this);
		this.layout.putConstraint( SpringLayout.NORTH, this.panelGauche, 5, SpringLayout.NORTH, this);
		this.layout.putConstraint( SpringLayout.SOUTH, this.panelGauche, -5, SpringLayout.SOUTH, this);
		this.add( this.panelGauche );

		this.panelDroit = new JPanel();
		this.layoutDroit = new SpringLayout();
		this.panelDroit.setLayout( this.layoutDroit );
		this.layout.putConstraint( SpringLayout.WEST, this.panelDroit, 5, SpringLayout.EAST, this.panelGauche );
		this.layout.putConstraint( SpringLayout.EAST, this.panelDroit, -5, SpringLayout.EAST, this );
		this.layout.putConstraint( SpringLayout.NORTH, this.panelDroit, 5, SpringLayout.NORTH, this);
		this.layout.putConstraint( SpringLayout.SOUTH, this.panelDroit, -5, SpringLayout.SOUTH, this);
		this.add( this.panelDroit );

		/**
		 * Onglets
		 */
		this.numOngletNotification = 4;


		/*
		 * ZONE CHRONO
		 */
		this.panelChrono = new JPanel();
		this.layoutDroit.putConstraint(SpringLayout.NORTH, this.panelChrono, 0, SpringLayout.NORTH, this.panelDroit );
		this.layoutDroit.putConstraint(SpringLayout.WEST, this.panelChrono, 0, SpringLayout.WEST, this.panelDroit );
		this.layoutDroit.putConstraint(SpringLayout.EAST, this.panelChrono, 0, SpringLayout.EAST, this.panelDroit );
		this.layoutDroit.putConstraint(SpringLayout.SOUTH, this.panelChrono, 150, SpringLayout.NORTH, this.panelChrono );
		this.panelDroit.add( this.panelChrono );

		this.layoutChrono = new SpringLayout();
		this.panelChrono.setLayout(this.layoutChrono);

		this.labelChrono = new JLabel();

		this.layoutChrono.putConstraint(SpringLayout.NORTH, this.labelChrono, 25, SpringLayout.NORTH, this.panelChrono );
		this.layoutChrono.putConstraint(SpringLayout.EAST, this.labelChrono, 5, SpringLayout.EAST, this.panelChrono );
		this.layoutChrono.putConstraint(SpringLayout.WEST, this.labelChrono, 5, SpringLayout.WEST, this.panelChrono );
		this.layoutChrono.putConstraint(SpringLayout.SOUTH, this.labelChrono, 70, SpringLayout.NORTH, this.labelChrono );
		this.labelChrono.setFont(new Font("Calibri", Font.PLAIN, 50));
		this.labelChrono.setText("00:00:00");
		this.labelChrono.setHorizontalAlignment(SwingConstants.CENTER);
		this.panelChrono.add( this.labelChrono);

		this.btnPause = new JButton(ConstantesAdmin.BOUTON_SUSPENDRE_PARTIE);
		this.layoutChrono.putConstraint(SpringLayout.NORTH, this.btnPause, 5, SpringLayout.SOUTH, this.labelChrono );
		this.layoutChrono.putConstraint(SpringLayout.WEST, this.btnPause, 73, SpringLayout.WEST, this.panelChrono );
		this.panelChrono.add( this.btnPause );
		this.btnPause.addActionListener( new ActionListener()
		{
			/**
			 * Detecte l'appuie sur le bouton de pause de la partie
			 * Stoppe le chrono
			 */
			@Override
			public void actionPerformed(ActionEvent e)
			{
				AdminPanel.this.controller.stopperPartie();
			}
		});

		this.btnPlay = new JButton(ConstantesAdmin.BOUTON_LANCEMENT_PARTIE);
		this.layoutChrono.putConstraint(SpringLayout.NORTH, this.btnPlay, 0, SpringLayout.NORTH, this.btnPause );
		this.layoutChrono.putConstraint(SpringLayout.WEST, this.btnPlay, 5, SpringLayout.EAST, this.btnPause );
		this.panelChrono.add( this.btnPlay );

		this.btnPlay.addActionListener( new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				AdminPanel.this.controller.lancerPartie();
			}
		});

		/*
		 * ZONE CHAT
		 */
		this.comboBox = new JComboBox();
		this.layoutDroit.putConstraint(SpringLayout.NORTH, this.comboBox, 5, SpringLayout.SOUTH, this.panelChrono);
		this.layoutDroit.putConstraint(SpringLayout.WEST, this.comboBox, 5, SpringLayout.WEST, this.panelDroit );
		this.layoutDroit.putConstraint(SpringLayout.EAST, this.comboBox, -5, SpringLayout.EAST, this.panelDroit );
		this.panelDroit.add(this.comboBox);

		this.textAreaChat = new JTextArea();
		JScrollPane scrollPane = new JScrollPane( this.textAreaChat );
		this.textAreaChat.setEditable( false );
		this.textAreaChat.setLineWrap( true );
		this.textAreaChat.setBackground( Color.white );
		this.layoutDroit.putConstraint(SpringLayout.NORTH, scrollPane, 6, SpringLayout.SOUTH, this.comboBox);
		this.layoutDroit.putConstraint(SpringLayout.WEST, scrollPane, 5, SpringLayout.WEST, this.panelDroit);
		this.layoutDroit.putConstraint(SpringLayout.SOUTH, scrollPane, 150, SpringLayout.NORTH, scrollPane);
		this.layoutDroit.putConstraint(SpringLayout.EAST, scrollPane, -5, SpringLayout.EAST, this.panelDroit);
		this.panelDroit.add( scrollPane );

		this.fieldChat = new JTextField();
		this.layoutDroit.putConstraint(SpringLayout.NORTH, this.fieldChat, 6, SpringLayout.SOUTH, scrollPane);
		this.layoutDroit.putConstraint(SpringLayout.WEST, this.fieldChat, 5, SpringLayout.WEST, this.panelDroit);
		this.layoutDroit.putConstraint(SpringLayout.EAST, this.fieldChat, -90, SpringLayout.EAST, this.panelDroit);
		this.panelDroit.add(this.fieldChat);

		this.buttonSend = new JButton("Envoyer");
		this.layoutDroit.putConstraint(SpringLayout.NORTH, this.buttonSend, -1, SpringLayout.NORTH, this.fieldChat);
		this.layoutDroit.putConstraint(SpringLayout.WEST, this.buttonSend, 6, SpringLayout.EAST, this.fieldChat);
		this.buttonSend.addActionListener( new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				AdminPanel.this.controller.send( AdminPanel.this.fieldChat.getText() );
				AdminPanel.this.fieldChat.setText( null );
			}
		});
		this.panelDroit.add(this.buttonSend);

		/*
		 * ZONE LOLCAM
		 */
		/*this.btnOn = new JButton("On");
		this.layoutDroit.putConstraint(SpringLayout.SOUTH, this.btnOn, -5, SpringLayout.SOUTH, this.panelDroit);
		this.layoutDroit.putConstraint(SpringLayout.EAST, this.btnOn, -5, SpringLayout.EAST, this.panelDroit);
		this.panelDroit.add(this.btnOn);*/


		/*
		 *
		 * ZONE GESTION CONTENU BDD
		 *
		 */

		/*
		 * CREATION SYSTEME D'ONGLETS
		 */
		this.tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		this.layoutGauche.putConstraint(SpringLayout.NORTH, this.tabbedPane, 5, SpringLayout.SOUTH, this.btnPlay);
		this.layoutGauche.putConstraint(SpringLayout.WEST, this.tabbedPane, 5, SpringLayout.WEST, this.panelGauche);
		this.layoutGauche.putConstraint(SpringLayout.SOUTH, this.tabbedPane, -10, SpringLayout.SOUTH, this.panelGauche);
		this.layoutGauche.putConstraint(SpringLayout.EAST, this.tabbedPane, -5, SpringLayout.EAST, this.panelGauche);



		/*
		 * ONGLET GESTION INDICES
		 */
		this.ongletIndice = new JPanel();
		this.tabbedPane.addTab(ConstantesAdmin.TITRE_ONGLET_INDICE, this.ongletIndice);
		this.sOngletIndice = new SpringLayout();
		this.ongletIndice.setLayout(this.sOngletIndice);

		this.btnNewIndice = new JButton("Ajouter Indice");
		this.sOngletIndice.putConstraint(SpringLayout.WEST, this.btnNewIndice, 10, SpringLayout.WEST, this.ongletIndice);
		this.sOngletIndice.putConstraint(SpringLayout.NORTH, this.btnNewIndice, 10, SpringLayout.NORTH, this.ongletIndice);
		/*
		 * Ajout
		 */
		this.btnNewIndice.addActionListener( new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				String[] temps = AdminPanel.this.labelChrono.getText().split(":");
				int dureePartie = Integer.parseInt( temps[0] ) * 60 + Integer.parseInt( temps[1] );
				System.out.println("TEMPS RESTANT : "+dureePartie);
				ArrayList<String> joueurs = AdminPanel.this.controller.getJoueurs();

				AjoutIndiceFrame fenetreIndice = new AjoutIndiceFrame( AdminPanel.this.controller, dureePartie , joueurs);
				fenetreIndice.setVisible( true );
			}
		});
		this.ongletIndice.add(this.btnNewIndice);

		this.scrollPane2 = new JScrollPane();
		this.sOngletIndice.putConstraint(SpringLayout.NORTH, this.scrollPane2, 10, SpringLayout.SOUTH, this.btnNewIndice);
		this.sOngletIndice.putConstraint(SpringLayout.WEST, this.scrollPane2, 10, SpringLayout.WEST, this.btnNewIndice);
		this.sOngletIndice.putConstraint(SpringLayout.SOUTH, this.scrollPane2, -80, SpringLayout.SOUTH, this.ongletIndice);
		this.sOngletIndice.putConstraint(SpringLayout.EAST, this.scrollPane2, -10, SpringLayout.EAST, this.ongletIndice);
		this.ongletIndice.add(this.scrollPane2);

		this.tableurIndice = new  NonEditableDefaultTableModel();
		this.tableurIndice.addColumn(ConstantesAdmin.COLONNES_INDICE[0]);
		this.tableurIndice.addColumn(ConstantesAdmin.COLONNES_INDICE[1]);
		this.tableurIndice.addColumn(ConstantesAdmin.COLONNES_INDICE[2]);
		this.tableurIndice.setRowCount(0);
		this.tableIndice = new JTable(this.tableurIndice);
		this.tableIndice.getColumnModel().getColumn(2).setMinWidth(0);
		this.tableIndice.getColumnModel().getColumn(2).setMaxWidth(0);
		this.tableIndice.getColumnModel().getColumn(2).setPreferredWidth(0);


		//Prix indice
		this.labelPrixIndice = new JLabel("Prix d'achat des indices ( en secondes ) : ");
		this.sOngletIndice.putConstraint(SpringLayout.WEST, this.labelPrixIndice, 0, SpringLayout.WEST, this.scrollPane2);
		this.sOngletIndice.putConstraint(SpringLayout.NORTH, this.labelPrixIndice, 20, SpringLayout.SOUTH, this.scrollPane2);
		this.ongletIndice.add( this.labelPrixIndice );

		this.textFieldPrix = new JTextField();
		this.sOngletIndice.putConstraint( SpringLayout.SOUTH, this.textFieldPrix, 3, SpringLayout.SOUTH, this.labelPrixIndice );
		this.sOngletIndice.putConstraint( SpringLayout.WEST, this.textFieldPrix, 10, SpringLayout.EAST, this.labelPrixIndice );
		this.sOngletIndice.putConstraint( SpringLayout.EAST, this.textFieldPrix, 50, SpringLayout.WEST, this.textFieldPrix );
		this.ongletIndice.add(this.textFieldPrix);

		this.btnValiderPrix = new JButton("Valider");
		this.sOngletIndice.putConstraint( SpringLayout.SOUTH, this.btnValiderPrix, 3, SpringLayout.SOUTH, this.labelPrixIndice );
		this.sOngletIndice.putConstraint( SpringLayout.WEST, this.btnValiderPrix, 10, SpringLayout.EAST, this.textFieldPrix );
		this.ongletIndice.add( this.btnValiderPrix );

		this.btnValiderPrix.addActionListener( new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				AdminPanel.netoyerInputEntier(AdminPanel.this.textFieldPrix);
				AdminPanel.this.controller.updatePrice(Integer.parseInt(AdminPanel.this.textFieldPrix.getText()));
			}
		});

		/*
		 * ONGLET GESTION ALERTES
		 */
		this.ongletAlerte = new JPanel();
		this.tabbedPane.addTab(ConstantesAdmin.TITRE_ONGLET_ALERTE, this.ongletAlerte);
		this.sOngletAlerte = new SpringLayout();
		this.ongletAlerte.setLayout(this.sOngletAlerte);

		this.btnNewAlerte = new JButton("Programmer une Alerte");
		this.sOngletAlerte.putConstraint(SpringLayout.NORTH, this.btnNewAlerte, 10, SpringLayout.NORTH, this.ongletAlerte);
		this.sOngletAlerte.putConstraint(SpringLayout.WEST, this.btnNewAlerte, 10, SpringLayout.WEST, this.ongletAlerte);
		this.ongletAlerte.add(this.btnNewAlerte);
		/*
		 * Ajout
		 */
		this.btnNewAlerte.addActionListener( new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				AjouterAlerteFrame fenetreAlerte = new AjouterAlerteFrame( AdminPanel.this.controller, AdminPanel.this.controller.getJoueurs() );
				fenetreAlerte.setVisible( true );
			}
		});

		this.scrollPane3 = new JScrollPane();
		this.sOngletAlerte.putConstraint(SpringLayout.NORTH, this.scrollPane3, 10, SpringLayout.SOUTH, this.btnNewAlerte);
		this.sOngletAlerte.putConstraint(SpringLayout.WEST, this.scrollPane3, 10, SpringLayout.WEST, this.btnNewAlerte);
		this.sOngletAlerte.putConstraint(SpringLayout.SOUTH, this.scrollPane3, -80, SpringLayout.SOUTH, this.ongletAlerte);
		this.sOngletAlerte.putConstraint(SpringLayout.EAST, this.scrollPane3, -10, SpringLayout.EAST, this.ongletAlerte);
		this.ongletAlerte.add(this.scrollPane3);

		this.tableurAlerte = new  NonEditableDefaultTableModel();
		this.tableurAlerte.addColumn(ConstantesAdmin.COLONNES_ALERTE[0]);
		this.tableurAlerte.addColumn(ConstantesAdmin.COLONNES_ALERTE[1]);
		this.tableurAlerte.addColumn(ConstantesAdmin.COLONNES_ALERTE[2]);
		this.tableurAlerte.addColumn(ConstantesAdmin.COLONNES_ALERTE[3]);
		this.tableurAlerte.setRowCount(0);
		this.tableAlerte = new JTable(this.tableurAlerte);


		this.scrollPane3.setViewportView(this.tableAlerte);
		/*
		 * Modifications
		 */
		this.tableAlerte.addMouseListener(new SelectionAlerte(this.tableAlerte, AdminPanel.this.controller));

		/*
		 * ONGLET GESTION JOUEURS
		 */
		this.ongletJoueur = new JPanel();
		this.ongletJoueur.setPreferredSize(new Dimension(300, 80));
		this.tabbedPane.addTab(ConstantesAdmin.TITRE_ONGLET_JOUEUR, this.ongletJoueur);
		this.sOngletJoueur = new SpringLayout();
		this.ongletJoueur.setLayout(this.sOngletJoueur);

		this.btnNewJoueur = new JButton("Ajouter Joueur");
		this.sOngletJoueur.putConstraint(SpringLayout.NORTH, this.btnNewJoueur, 10, SpringLayout.NORTH, this.ongletJoueur);
		this.sOngletJoueur.putConstraint(SpringLayout.WEST, this.btnNewJoueur, 10, SpringLayout.WEST, this.ongletJoueur);
		/*
		 * Ajout
		 */
		this.btnNewJoueur.addActionListener( new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				AjoutJoueurFrame fenetreJoueur = new AjoutJoueurFrame( AdminPanel.this.controller );
				fenetreJoueur.setVisible( true );
			}
		});
		this.ongletJoueur.add(this.btnNewJoueur);

		this.scrollPane1 = new JScrollPane();
		this.sOngletJoueur.putConstraint(SpringLayout.NORTH, this.scrollPane1, 10, SpringLayout.SOUTH, this.btnNewJoueur);
		this.sOngletJoueur.putConstraint(SpringLayout.WEST, this.scrollPane1, 10, SpringLayout.WEST, this.btnNewJoueur);
		this.sOngletJoueur.putConstraint(SpringLayout.SOUTH, this.scrollPane1, -80, SpringLayout.SOUTH, this.ongletJoueur);
		this.sOngletJoueur.putConstraint(SpringLayout.EAST, this.scrollPane1, -10, SpringLayout.EAST, this.ongletJoueur);
		this.ongletJoueur.add(this.scrollPane1);
		this.tableurJoueur = new  NonEditableDefaultTableModel();
		this.tableurJoueur.addColumn(ConstantesAdmin.COLONNES_JOUEUR[0]);
		this.tableurJoueur.addColumn(ConstantesAdmin.COLONNES_JOUEUR[1]);
		this.tableurJoueur.addColumn(ConstantesAdmin.COLONNES_JOUEUR[2]);
		this.tableurJoueur.addColumn(ConstantesAdmin.COLONNES_JOUEUR[3]);
		this.tableurJoueur.setRowCount(0);

		this.tableJoueur = new JTable(this.tableurJoueur);
		/*
		 * Modifications
		 */
		this.tableJoueur.addMouseListener(new SelectionJoueur(this.tableJoueur, AdminPanel.this.controller));

		this.listSelectionModelJoueur = this.tableJoueur.getSelectionModel();
		this.scrollPane1.setViewportView(this.tableJoueur);

		/*
		 * ONGLET SCENARIO
		 */
		this.ongletScenario = new JPanel();
		this.tabbedPane.addTab(ConstantesAdmin.TITRE_ONGLET_SCENARIO, this.ongletScenario);
		this.sOngletScenario = new SpringLayout();
		this.ongletScenario.setLayout(this.sOngletScenario);

		this.btnSaveScenario = new JButton("Enregistrer");
		this.sOngletScenario.putConstraint(SpringLayout.SOUTH, this.btnSaveScenario, -5, SpringLayout.SOUTH, this.ongletScenario );
		this.sOngletScenario.putConstraint(SpringLayout.EAST, this.btnSaveScenario, -5, SpringLayout.EAST, this.ongletScenario );
		this.ongletScenario.add(this.btnSaveScenario);
		/*
		 * Ajout
		 */
		this.btnSaveScenario.addActionListener( new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent arg0)
			{

				AdminPanel.this.netoyerInputsTemps();

				AdminPanel.this.setTime( Integer.parseInt(AdminPanel.this.fieldHeure.getText()) * 3600 + Integer.parseInt( AdminPanel.this.fieldMinute.getText() ) * 60 );

				AdminPanel.this.controller.saveScenario
						(
							AdminPanel.this.txtScenario.getText(),
							AdminPanel.this.fieldHeure.getText(),
							AdminPanel.this.fieldMinute.getText()
						);
			}
		});

		this.labelDureePartie = new JLabel( "Durée de la partie (en heure : minute) :" );
		this.sOngletScenario.putConstraint(SpringLayout.SOUTH, this.labelDureePartie, -5, SpringLayout.NORTH, this.btnSaveScenario);
		this.sOngletScenario.putConstraint(SpringLayout.WEST, this.labelDureePartie, 5, SpringLayout.WEST, this.ongletScenario);
		this.ongletScenario.add(this.labelDureePartie);

		this.fieldHeure = new JFormattedTextField();
		this.fieldHeure.setText("00");
		this.sOngletScenario.putConstraint(SpringLayout.WEST, this.fieldHeure, 10, SpringLayout.EAST, this.labelDureePartie );
		this.sOngletScenario.putConstraint(SpringLayout.EAST, this.fieldHeure, 25, SpringLayout.WEST, this.fieldHeure );
		this.sOngletScenario.putConstraint(SpringLayout.SOUTH, this.fieldHeure, 0, SpringLayout.SOUTH, this.labelDureePartie );
		this.ongletScenario.add(this.fieldHeure);

		this.fieldMinute = new JFormattedTextField();
		this.fieldMinute.setText("00");
		this.sOngletScenario.putConstraint(SpringLayout.SOUTH, this.fieldMinute, 0, SpringLayout.SOUTH, this.fieldHeure);
		this.sOngletScenario.putConstraint(SpringLayout.EAST, this.fieldMinute, 25, SpringLayout.WEST, this.fieldMinute );
		this.sOngletScenario.putConstraint(SpringLayout.WEST, this.fieldMinute, 10, SpringLayout.EAST, this.fieldHeure );
		this.ongletScenario.add(this.fieldMinute);

		this.txtScenario =new JTextArea();
		this.scrollPane4 = new JScrollPane(this.txtScenario);

		this.sOngletScenario.putConstraint(SpringLayout.NORTH, this.scrollPane4, 5, SpringLayout.NORTH, this.ongletScenario);
		this.sOngletScenario.putConstraint(SpringLayout.WEST, this.scrollPane4, 5, SpringLayout.WEST, this.ongletScenario);
		this.sOngletScenario.putConstraint(SpringLayout.SOUTH, this.scrollPane4, -10, SpringLayout.NORTH, this.labelDureePartie );
		this.sOngletScenario.putConstraint(SpringLayout.EAST, this.scrollPane4, -5, SpringLayout.EAST, this.ongletScenario);
		this.ongletScenario.add(this.scrollPane4);


		/*
		 *  TODO : Notifications : Constantes admin
		 */

		this.ongletNotifications = new JPanel();
		this.tabbedPane.addTab(ConstantesAdmin.TITRE_ONGLET_NOTIFICATION, this. ongletNotifications);
		this.sOngletNotifications = new SpringLayout();
		this.ongletNotifications.setLayout(this.sOngletNotifications);

	    this.notificationBar = new NotificationPanel();
		this.notifPane = new JScrollPane(this.notificationBar);

		this.sOngletNotifications.putConstraint(SpringLayout.NORTH, this.notifPane, 10, SpringLayout.NORTH, this.ongletNotifications);
		this.sOngletNotifications.putConstraint(SpringLayout.WEST, this.notifPane, 10, SpringLayout.WEST, this.ongletNotifications);
		this.sOngletNotifications.putConstraint(SpringLayout.SOUTH, this.notifPane, -10, SpringLayout.SOUTH, this.ongletNotifications);
		this.sOngletNotifications.putConstraint(SpringLayout.EAST, this.notifPane, -10, SpringLayout.EAST, this.ongletNotifications);
		this.ongletNotifications.add(this.notifPane);

		/*
		 * Modification Indice
		 */
		this.tableIndice.addMouseListener(new SelectionIndice(
											this.tableIndice,
											AdminPanel.this.controller,
											this.labelChrono.getText()
											));
		this.scrollPane2.setViewportView(this.tableIndice);

		this.tabbedPane.setOpaque(true);
		this.panelGauche.add(this.tabbedPane);
	}

	/**
	 * Met à jour le contenu de la table joueur
	 * @param data Données à afficher dans data
	 */
	public void appendTableJoueur(String[] data)
	{
		this.tableurJoueur.addRow(data);
	}

	/**
	 * Met à jour le contenu de la table indice
	 * @param data Données à afficher dans data
	 */
	public void appendTableIndice(String[] data)
	{
		this.tableurIndice.addRow(data);
	}

	/**
	 * Met à jour le contenu de la table joueur
	 * @param data Données à afficher dans data
	 */
	public void appendTableAlerte(String[] data)
	{
		this.tableurAlerte.addRow(data);
	}

	public void updateOnglets( AdminModel pModel, int pStatut )
	{
		if ( this.ongletAlerte.isVisible()
			 || pStatut == AdminView.REFRESH_ALERTE
			 || pStatut == AdminView.REFRESH_ALL
			 && ! this.tableIndice.isFocusOwner())
		{
			this.tableurAlerte.setRowCount(0);
			ArrayList<String[]> alertes = pModel.getAlertes();
			for(String[] str : alertes)
			{
				this.appendTableAlerte(str);
			}
		}

		if ( (this.ongletIndice.isVisible()
			 || pStatut == AdminView.REFRESH_INDICE
			 || pStatut == AdminView.REFRESH_ALL)
			 && ! this.tableIndice.isFocusOwner() )
		{
			this.tableurIndice.setRowCount(0);
			ArrayList<String[]> indices = pModel.getIndices();
			for(String[] str : indices)
			{
				this.appendTableIndice(str);
			}
		}

		if ( this.ongletJoueur.isVisible()
			 || pStatut == AdminView.REFRESH_JOUEUR
			 || pStatut == AdminView.REFRESH_ALL
			 && ! this.tableIndice.isFocusOwner() )
		{
			this.tableurJoueur.setRowCount(0);
			ArrayList<String[]> joueurs = pModel.getJoueurs();
			for(String[] str : joueurs)
			{
				this.appendTableJoueur(str);
			}
		}

		if ( pStatut == AdminView.REFRESH_SCENARIO
			 || pStatut == AdminView.REFRESH_ALL )
		{
			this.txtScenario.setText( pModel.getScenario().getTexte() );
		}

		this.updateNotification(pModel);
	}

	/**
	 * Mise à jour du système de notifications
	 * @param pModel
	 */
	private void updateNotification(AdminModel pModel) {
		if ( this.ongletNotifications.isVisible())
		{
				this.notificationBar.toSelect();
		}
		else{
			this.notificationBar.resetNbNotifs();
		}
		this.notificationBar.clearNotifications();
		ArrayList<Evenement> notifications = pModel.getNotifications();
		for(Evenement evt : notifications)
		{
			this.notificationBar.addBar(evt);
		}
		if(this.notificationBar.getNbNotifs() >0)
		{
			this.notificationBar.putSound();
			this.tabbedPane.setTitleAt(this.numOngletNotification, ConstantesAdmin.TITRE_ONGLET_NOTIFICATION+" (" + this.notificationBar.getNbNotifs() + ")");
		}
		else
		{
			this.tabbedPane.setTitleAt(this.numOngletNotification, ConstantesAdmin.TITRE_ONGLET_NOTIFICATION);
		}
	}

	public JTextArea getTextAreaChat()
	{
		return this.textAreaChat;
	}

	public void setScenario( int pTempsRestant )
	{
		this.setTime( pTempsRestant );
	}

	/**
	 * Met à jour tous les temps présent dans la vue
	 * @param pSecondesRestantes
	 */
	public void setTime( int pSecondesRestantes )
	{
		// Décomposition en heures/minutes/secondes
		int nbHeure = pSecondesRestantes / 3600;
		pSecondesRestantes = pSecondesRestantes % 3600;
		int nbMinute = pSecondesRestantes / 60;
		pSecondesRestantes = pSecondesRestantes % 60;

		// Affichage dans l'onglet scénario,
		if ( ! this.fieldHeure.isFocusOwner() && ! this.fieldMinute.isFocusOwner() )
		{
			this.fieldHeure.setText( Outils.zeroDizaine(nbHeure) + nbHeure );
			this.fieldMinute.setText( Outils.zeroDizaine(nbMinute) + nbMinute );
		}

		// Affichage du label général
		this.labelChrono.setText(
									Outils.zeroDizaine(nbHeure) + nbHeure + ":" +
									Outils.zeroDizaine(nbMinute) + nbMinute + ":" +
									Outils.zeroDizaine(pSecondesRestantes) + pSecondesRestantes
								);
	}

	public void setPrice(int pPrix)
	{
		if ( ! this.textFieldPrix.isFocusOwner() )
		{
			this.textFieldPrix.setText(pPrix+"");
		}
	}

	/**
	 * Nettoie les champs pour entrer des entiers
	 */
	protected void netoyerInputsTemps()
	{
		netoyerInputEntier( this.fieldHeure );
		netoyerInputEntier( this.fieldMinute );
	}

	/**
	 * Nettoie un input d'entier
	 */
	public static void netoyerInputEntier( JTextField pField )
	{
		if ( pField.getText().equals("") )
		{
			pField.setText("00");
		}
		else
		{
			char[] inputString = pField.getText().toCharArray();
			String outputString = "";
			for( int i = 0; i < inputString.length; i++ )
			{
				if ( Character.isDigit( inputString[i] ) )
				{
					outputString += inputString[i];
				}
			}
			pField.setText( outputString );
		}
	}
}
